package com.k2.exstrasensorytypredictor.controls;

import java.io.IOException;
import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.k2.exstrasensorytypredictor.interfaces.IConstants;
import com.k2.exstrasensorytypredictor.interfaces.ITouchHandler;

public class MButton extends View implements IConstants{
	public static final int UNFOCUSED_STATE = 0;
	public static final int FOCUSED_STATE = 1;
	
	private int id;
	private int color;
	private int[] paddings;
	
	private float width;
	private float height;
	private float textSize;
	private float skoef = 0.0f;
	
	private Matrix matrix;
	
	private float[] koef;
	
	private Bitmap[] bitmaps;
	private Bitmap current;
	
	private String name;
	private String text;
	
	private TextView caption;
	
	private ITouchHandler handler;
	
	private Context parentContext;
	
	private boolean isFocused;
	
	private Paint paint;
	
	private StaticLayout textLayout;

	public MButton(Context context, String[] gfx, int id) {
		super(context);
		handler = (ITouchHandler) context;
		this.parentContext = context;
		this.id = id;
		try {
			bitmaps = new Bitmap[]{BitmapFactory.decodeStream(context.getAssets().open(gfx[0])), BitmapFactory.decodeStream(context.getAssets().open(gfx[1]))};
			if (bitmaps[0] != null || bitmaps[1] != null){
				current = bitmaps[UNFOCUSED_STATE];
			}
			width = current.getWidth();
			height = current.getHeight();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		paint = new Paint();
	}
	
	public MButton(Context context, String[] gfx, int id, boolean isNeedScale) {
		super(context);
		handler = (ITouchHandler) context;
		this.id = id;
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		float w = (float) display.getWidth();
		float h = (float) display.getHeight();
		this.koef = new float[2];
		this.koef[0] = w / SCREEN_RES_X;
		this.koef[1] = h / SCREEN_RES_Y;
		this.matrix = new Matrix();
		try {
			bitmaps = new Bitmap[]{BitmapFactory.decodeStream(context.getAssets().open(gfx[0])), BitmapFactory.decodeStream(context.getAssets().open(gfx[1]))};
			if (bitmaps[0] != null || bitmaps[1] != null){
				current = bitmaps[UNFOCUSED_STATE];
			}
			width = current.getWidth();
			height = current.getHeight();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		paint = new Paint();
	}
	
	public MButton(Context context, Bitmap[] gfx, int id, boolean isNeedScale) {
		super(context);
		handler = (ITouchHandler) context;
		this.id = id;
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		float w = (float) display.getWidth();
		float h = (float) display.getHeight();
		this.koef = new float[2];
		this.koef[0] = w / SCREEN_RES_X;
		this.koef[1] = h / SCREEN_RES_Y;
		this.matrix = new Matrix();
		bitmaps = gfx;
		if (bitmaps[0] != null || bitmaps[1] != null){
			current = bitmaps[UNFOCUSED_STATE];
		}
		width = current.getWidth();
		height = current.getHeight();
	}
	
	public MButton(Context context, Bitmap[] gfx, int id, boolean isNeedScale, float sKoeff) {
		super(context);
		this.skoef = sKoeff;
		handler = (ITouchHandler) context;
		this.id = id;
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		Display display = wm.getDefaultDisplay();
		float w = (float) display.getWidth();
		float h = (float) display.getHeight();
		this.koef = new float[2];
		this.koef[0] = w / SCREEN_RES_X;
		this.koef[1] = h / SCREEN_RES_Y;
		this.matrix = new Matrix();
		bitmaps = gfx;
		if (bitmaps[0] != null || bitmaps[1] != null){
			current = bitmaps[UNFOCUSED_STATE];
		}
		width = current.getWidth()*sKoeff;
		height = current.getHeight()*sKoeff;
	}
	
	public void setBitmaps(Bitmap[] gfx){
		this.bitmaps = gfx;
		this.current = this.bitmaps[UNFOCUSED_STATE];
		invalidate();
	}
	
	public Bitmap[] getBitmaps() {
		return bitmaps;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (!isFocused){
			current = bitmaps[UNFOCUSED_STATE];
		} else {
			current = bitmaps[FOCUSED_STATE];
		}
		if (this.matrix != null){
			matrix.setScale(this.koef[0]-skoef, this.koef[1]-skoef);
			canvas.drawBitmap(current, this.matrix, null);
		} else {
			canvas.drawBitmap(current, 0.0f, 0.0f, null);
		}
		
		if (this.textLayout != null){
			textLayout.draw(canvas);
		}
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN){
			Log.d("btn_touch_event", "DOWN");
			isFocused = true;
			invalidate();
			return true;
		} else if (event.getAction() == MotionEvent.ACTION_UP){
			Log.d("btn_touch_event", "UP");
			isFocused = false;
			invalidate();
			handler.onAreaTouched(this, event);
			return true;
		}
		
		return false;
	}
	
	public float getBtnWidth() {
		return width * this.koef[0];
	}

	public float getBtnHeight() {
		return height * this.koef[1];
	}
	
	public String getName(){
		return name;
	}
	
	public void setName(String btnName){
		name = btnName;
	}
	
	public int getId(){
		return this.id;
	}
	
	@Override
	protected void onMeasure(int w, int h) {
		setMeasuredDimension((int)getBtnWidth(), (int)getBtnHeight());
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
		TextPaint paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
		paint.setColor(Color.BLACK);
		paint.setTextSize(getTextSize());
		paint.setAntiAlias(true);
		this.textLayout = new StaticLayout(text, paint, (int)getBtnWidth(), Layout.Alignment.ALIGN_CENTER, 1.3f, 0.0f, false);
	}

	public int getColor() {
		return color;
	}

	public void setColor(int color) {
		this.color = color;
	}

	public int[] getPaddings() {
		return paddings;
	}

	public void setPaddings(int[] paddings) {
		this.paddings = paddings;
		setPadding(paddings[0], paddings[1], 0, 0);
	}

	public float getTextSize() {
		return textSize;
	}

	public void setTextSize(float textSize) {
		this.textSize = textSize;
	}
}
